//
//  XVCCollectPocket.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/31.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCCollectPocket.h"
#import "XXTool.h"
#import "XTCCollectVisitors.h"
#import "ProductInformationShowCell.h"
#import "ShowExhibitorNameCell.h"

#import "ExhibitorsDetailsViewController.h"
#import "XVCProductDetailShow.h"
#import "XMLExhibitorShowModel.h"

@interface XVCCollectPocket ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableview;

@property (nonatomic,strong) DKButton *productButton;

@property (nonatomic,strong) DKButton *visitorOrExhibitorButton;

@property (nonatomic,strong) UIView *line;

@property (nonatomic,strong) UIView *noCollect;

@end

@implementation XVCCollectPocket
{
    NSMutableArray <RXMLExhibitorInformationModel*> *exhibitorArray;
    NSMutableArray <XMLRealsePersonInformation*> *vistorArray;
    NSMutableArray <XMLProductInformationShowModel*> *productArray;
    BOOL is_productShow;
    BOOL isExhibitor;
    BOOL isFirstPressRightButton;
    
    DKSortArray *sortUse;
    NSMutableArray *sortRule;
    
    MJRefreshBackNormalFooter *useFooter;
}


-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.backgroundColor = [XXTool colorWithBackGround];
        _tableview.separatorColor = [XXTool colorWithLine];
        _tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableview.sectionIndexColor = [XXTool colorWithProject];
        _tableview.sectionIndexBackgroundColor = [UIColor clearColor];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        MJRefreshNormalHeader *useHead = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        [useHead setTitle:NSLocalizedString(@"下拉刷新", nil) forState:MJRefreshStateIdle];
        [useHead setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useHead setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_header = useHead;
        
        useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerFresh)];
        [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
        [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_footer = useFooter;
        
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(UIView*)noCollect
{
    if (_noCollect==nil)
    {
        _noCollect = [[UIView alloc] init];
        _noCollect.backgroundColor = [UIColor clearColor];
        _noCollect.clipsToBounds = YES;
        [_tableview addSubview:_noCollect];
        
        UIImageView *noImg = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-90)/2
                                                                           , 100
                                                                           , 90
                                                                           , 90)];
        noImg.image = [[UIImage imageNamed:@"收藏缺省"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        noImg.tintColor = [XXTool colorProjectWithAlpha:0.7];
        noImg.contentMode = UIViewContentModeScaleAspectFit;
        [_noCollect addSubview:noImg];
        
        UILabel *noLabel = [[UILabel alloc] initWithFrame:CGRectMake(0
                                                                     , noImg.frame.size.height+noImg.frame.origin.y+15
                                                                     , self.view.frame.size.width
                                                                     , 18)];
        noLabel.font = [XXTool boldSystemFontOfSize:16];
        noLabel.textColor = [XXTool colorProjectWithAlpha:0.7];
        noLabel.textAlignment = NSTextAlignmentCenter;
        noLabel.text = NSLocalizedString(@"暂时没有被收藏的内容", nil);
        [_noCollect addSubview:noLabel];
        _noCollect.hidden = YES;
    }
    return _noCollect;
}

-(DKButton*)productButton
{
    if (_productButton==nil)
    {
        _productButton = [[DKButton alloc] init];
        _productButton.text = NSLocalizedString(@"展品", nil);
        _productButton.font = [XXTool systemFontOfSize:15];
        _productButton.textColor = [UIColor whiteColor];
        [_productButton addTarget:self Select:@selector(showProduct) ForEvent:DKButtonEventPressedEnd];
        [_productButton setColor:[XXTool colorWithProject] ForState:DKButtonStateNormal];
        [self.view addSubview:_productButton];
    }
    return _productButton;
}

-(DKButton*)visitorOrExhibitorButton
{
    if (_visitorOrExhibitorButton==nil)
    {
        _visitorOrExhibitorButton = [[DKButton alloc] init];
        _visitorOrExhibitorButton.text = NSLocalizedString(@"观众", nil);
        _visitorOrExhibitorButton.font = [XXTool systemFontOfSize:15];
        _visitorOrExhibitorButton.textColor = [XXTool colorWithProject];
        [_visitorOrExhibitorButton addTarget:self Select:@selector(showVisitorOrExhibitor) ForEvent:DKButtonEventPressedEnd];
        [_visitorOrExhibitorButton setColor:[UIColor whiteColor] ForState:DKButtonStateNormal];
        [self.view addSubview:_visitorOrExhibitorButton];
    }
    return _visitorOrExhibitorButton;
}

-(UIView*)line
{
    if (_line==nil)
    {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [XXTool colorWithProject];
        [self.view addSubview:_line];
    }
    return _line;
}

-(void)initParameter
{
    isExhibitor = [UserInfo shareUserInfo].isExhibitor;
    isFirstPressRightButton = YES;
    
    productArray = [NSMutableArray array];
    vistorArray = [NSMutableArray array];
    exhibitorArray = [NSMutableArray array];
}

-(void)initUI
{
    [self.navigationItem setTitle:NSLocalizedString(@"收藏夹", nil)];
    
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"更多"] style:UIBarButtonItemStylePlain target:self action:@selector(clickleft)]];
    [self productButton];
    [self visitorOrExhibitorButton];
    [self line];
    [self tableview];
    [self noCollect];
    
    _productButton.frame = CGRectMake(0, 0, self.view.frame.size.width/2
                                      , 43.5f);
    _visitorOrExhibitorButton.frame = CGRectMake(self.view.frame.size.width/2
                                                 , 0
                                                 , _productButton.frame.size.width
                                                 , _productButton.frame.size.height);
    _line.frame = CGRectMake(0, _visitorOrExhibitorButton.frame.size.height+_visitorOrExhibitorButton.frame.origin.y
                             , self.view.frame.size.width
                             , 1);
    _tableview.frame = CGRectMake(0, _line.frame.size.height+_line.frame.origin.y, self.view.frame.size.width
                                  , self.view.frame.size.height-(_line.frame.size.height+_line.frame.origin.y));
    
    is_productShow = YES;
    if (isExhibitor)
    {
        _visitorOrExhibitorButton.text = NSLocalizedString(@"观众", nil);
    }
    else
    {
        _visitorOrExhibitorButton.text = NSLocalizedString(@"展商", nil);
    }
}

-(void)initData
{
    [_tableview startLoading];
    [self getData];
}

-(void)refreshLayout:(CGRect)frame
{
    _productButton.frame = CGRectMake(0, 0, frame.size.width/2
                                      , 43.5f);
    _visitorOrExhibitorButton.frame = CGRectMake(frame.size.width/2
                                                 , 0
                                                 , _productButton.frame.size.width
                                                 , _productButton.frame.size.height);
    _line.frame = CGRectMake(0, _visitorOrExhibitorButton.frame.size.height+_visitorOrExhibitorButton.frame.origin.y
                             , frame.size.width
                             , 1);
    
    _tableview.frame = CGRectMake(0, _line.frame.size.height+_line.frame.origin.y, frame.size.width
                                       , frame.size.height-(_line.frame.size.height+_line.frame.origin.y));
    
    _noCollect.frame = CGRectMake(0, 0, _tableview.frame.size.width
                                  , _tableview.frame.size.height);
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_tableview.mj_header beginRefreshing];
}

-(void)getData
{
    if (is_productShow)
    {
        [XMLProductInformationShowModelArray requestProductCollectListWithListCount:0
                                                                         completion:^(BOOL isSuccess, NSMutableArray<XMLProductInformationShowModel *> *datalist) {
                                                                             if (isSuccess)
                                                                             {
                                                                                 productArray = datalist;
                                                                                 
                                                                                 
                                                                             }
                                                                             if (productArray.count==0)
                                                                             {
                                                                                 _noCollect.hidden = NO;
                                                                             }
                                                                             else
                                                                             {
                                                                                 _noCollect.hidden = YES;
                                                                             }
                                                                             [_tableview reloadData];
                                                                             [_tableview stopLoading];
                                                                             [_tableview.mj_header endRefreshing];
                                                                         }];
    }
    else if (isExhibitor)
    {
        [XMLRealsePersonInformation requestExhibitorCollectVisitorListWithListCount:0
                                                                         completion:^(BOOL isSuccess, NSMutableArray<XMLRealsePersonInformation *> *dataList) {
                                                                             if (isSuccess)
                                                                             {
                                                                                 vistorArray = dataList;
                                                                                 _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
                                                                                 
                                                                                 
                                                                                 
                                                                             }
                                                                             if (vistorArray.count==0)
                                                                             {
                                                                                 _noCollect.hidden = NO;
                                                                             }
                                                                             else
                                                                             {
                                                                                 _noCollect.hidden = YES;
                                                                             }
                                                                             [_tableview reloadData];
                                                                             [_tableview stopLoading];
                                                                             [_tableview.mj_header endRefreshing];
                                                                         }];
    }
    else
    {
        [XMLExhibitorShowModel requestVisitorCollectExihibitorListWithCompletion:^(BOOL isSuccess, NSMutableArray<RXMLExhibitorInformationModel *> *datalist) {
            if (isSuccess)
            {
    
                exhibitorArray = datalist;
                
                _tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
                
                sortUse = [[DKSortArray alloc] init];
                sortUse.dataSource = self;
                
                sortRule = [[NSMutableArray alloc] init];
                
                if ([XXTool didCurrentLanguageChinese])
                {
                    for (RXMLExhibitorInformationModel *temp in exhibitorArray)
                    {
                        [sortRule addObject:temp.CompanyName_chn];
                    }
                }
                else
                {
                    for (RXMLExhibitorInformationModel *temp in exhibitorArray)
                    {
                        [sortRule addObject:temp.CompanyName_eng];
                    }
                }
                [sortUse setArray:sortRule];
                [sortUse sort];
                
                
            }
            
            if (exhibitorArray.count==0)
            {
                _noCollect.hidden = NO;
            }
            else
            {
                _noCollect.hidden = YES;
            }
            [_tableview reloadData];
            [_tableview stopLoading];
            [_tableview.mj_header endRefreshing];
                 
        }];
    }
}

-(void)footerFresh
{
    if (is_productShow)
    {
        [XMLProductInformationShowModelArray requestProductCollectListWithListCount:productArray.count
                                                                         completion:^(BOOL isSuccess, NSMutableArray<XMLProductInformationShowModel *> *datalist) {
                                                                             if (isSuccess)
                                                                             {
                                                                                 if (datalist.count==0)
                                                                                 {
                                                                                     
                                                                                 }
                                                                                 else
                                                                                 {
                                                                                     [productArray addObjectsFromArray:datalist];
                                                                                 }
                                                                             }
                                                                             [_tableview reloadData];
                                                                             [_tableview.mj_footer endRefreshing];
                                                                         }];
    }
    else if (isExhibitor)
    {
        [XMLRealsePersonInformation requestExhibitorCollectVisitorListWithListCount:vistorArray.count
                                                                         completion:^(BOOL isSuccess, NSMutableArray<XMLRealsePersonInformation *> *dataList) {
                                                                             if (isSuccess)
                                                                             {
                                                                                 if (dataList.count)
                                                                                 {
                                                                                     
                                                                                 }
                                                                                 else
                                                                                 {
                                                                                     [vistorArray addObjectsFromArray:dataList];
                                                                                     
                                                                                 }
                                                                             }
                                                                             [_tableview reloadData];
                                                                             [_tableview.mj_footer endRefreshing];
                                                                         }];
    }
    else
    {
        
    }
}

-(void)clickleft
{
    //[self.navigationController.sideViewController showLeftView];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)showProduct
{
    [_visitorOrExhibitorButton setColor:[UIColor whiteColor] ForState:DKButtonStateNormal];
    [_productButton setColor:[XXTool colorWithProject] ForState:DKButtonStateNormal];
    _visitorOrExhibitorButton.textColor = [XXTool colorWithProject];
    _productButton.textColor = [UIColor whiteColor];
    is_productShow = YES;
    _tableview.mj_footer = useFooter;
    
    if (productArray.count==0)
    {
        _noCollect.hidden = NO;
    }
    else
    {
        _noCollect.hidden = YES;
    }
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableview reloadData];
    
}

-(void)showVisitorOrExhibitor
{
    [_productButton setColor:[UIColor whiteColor] ForState:DKButtonStateNormal];
    [_visitorOrExhibitorButton setColor:[XXTool colorWithProject] ForState:DKButtonStateNormal];
    _productButton.textColor = [XXTool colorWithProject];
    _visitorOrExhibitorButton.textColor = [UIColor whiteColor];
    is_productShow = NO;
    
    
    if (isFirstPressRightButton)
    {
        [_tableview startLoading];
        if (isExhibitor)
        {
            _tableview.mj_footer = useFooter;
        }
        else
        {
            _tableview.mj_footer = nil;
        }
        [self getData];
        isFirstPressRightButton = NO;
    }
    else
    {
        if (isExhibitor)
        {
            if (vistorArray.count==0)
            {
                _noCollect.hidden = NO;
            }
            else
            {
                _noCollect.hidden = YES;
            }
            
            _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
            _tableview.mj_footer = useFooter;
        }
        else
        {
            
            if (exhibitorArray.count==0)
            {
                _noCollect.hidden = NO;
            }
            else
            {
                _noCollect.hidden = YES;
            }

            _tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
            _tableview.mj_footer = nil;
        }
        [_tableview reloadData];
    }
}

#pragma mark DKSortArrayDatasource
-(NSString*)DKSortArray:(DKSortArray *)object stringOfSortArray:(NSArray *)dataArray AtIndex:(NSUInteger)index
{
    return sortRule[index];
}

#pragma mark tableviewDatasource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (is_productShow)
    {
        ProductInformationShowCell *cell = [ProductInformationShowCell cellSelectionStyleNoneWithTableView:tableView];
        [cell setCellWithData:productArray[indexPath.row]];
        return cell;
    }
    else if (isExhibitor)
    {
        XTCCollectVisitors *cell = [XTCCollectVisitors cellSelectionStyleNoneWithTableView:tableView];
        [cell setCellData:vistorArray[indexPath.row]];
        return cell;
    }
    else
    {
        ShowExhibitorNameCell *cell = [ShowExhibitorNameCell cellWithTableView:tableView];
        [cell setCellWithData:exhibitorArray[[sortUse indexOfSection:indexPath.section AtIndex:indexPath.row]]];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (is_productShow)
    {
        return 180;
    }
    else if (isExhibitor)
    {
        return [XTCCollectVisitors getHeightWithData:vistorArray[indexPath.row]];
    }
    else
    {
        return [ShowExhibitorNameCell getCellHeightWithData:exhibitorArray[[sortUse indexOfSection:indexPath.section AtIndex:indexPath.row]]];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (isExhibitor||is_productShow)
    {
        return 1;
    }
    else
    {
        return [sortUse numberOfSection];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (is_productShow)
    {
        return productArray.count;
    }
    else if (isExhibitor)
    {
        return vistorArray.count;
    }
    else
    {
        return [sortUse numberOfRowAtSection:section];
    }
}

-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (isExhibitor||is_productShow)
    {
        return nil;
    }
    else
    {
        return [sortUse getTitleArray];
    }
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (isExhibitor||is_productShow)
    {
        return @"";
    }
    else
    {
        return [[sortUse getTitleArray] objectAtIndex:section];
    }
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (isExhibitor||is_productShow)
    {
        return nil;
    }
    else
    {
        NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
        if (sectionTitle == nil) {
            return  nil;
        }
        
        UILabel * label = [[UILabel alloc] init];
        label.frame = CGRectMake(15, 0, self.view.frame.size.width-15, 35/2);
        label.font=[XXTool systemFontOfSize:14];
        label.text = sectionTitle;
        
        UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0
                                                                        , 0
                                                                        , self.view.frame.size.width-15
                                                                        , 35/2)];
        [sectionView setBackgroundColor:[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0]];
        [sectionView addSubview:label];
        return sectionView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (isExhibitor||is_productShow)
    {
        return 0;
    }
    else
    {
        return 35/2;
    }
}


#pragma mark tableviewDelgate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (is_productShow)
    {
        XMLProductInformationShowModel *temp = productArray[indexPath.row];
        NSString *exName = @"";
        if ([XXTool didCurrentLanguageChinese])
        {
            exName = temp.cpname;
        }
        else
        {
            exName = temp.cpname_en;
        }
        [XVCProductDetailShow pushProductDetailVCWithSourceVC:self goods_id:temp.goods_id exhibitorName:exName];
    }
    else if (isExhibitor)
    {
        
    }
    else
    {
        [ExhibitorsDetailsViewController pushViewControllerWithSourceVC:self exhibitorInfo:exhibitorArray[[sortUse indexOfSection:indexPath.section AtIndex:indexPath.row]]];
    }
}

@end
